<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2017-08-30
 * Time: 0:09
 */

namespace app\models;


use app\librarys\DbCacheUtility;
use app\librarys\FileService;
use app\librarys\Tools;

class SaleArticle
{
    public static function getCount($title)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT count(1) FROM t_sale_article WHERE title like :title');
        $cmd->bindValue(':title', '%' . $title . '%');
        return DbCacheUtility::queryScalar($cmd);
    }

    public static function getList($page, $pageSize = 20, $title)
    {
        $sql = 'SELECT * FROM t_sale_article WHERE  title like :title
                ORDER BY create_datetime DESC 
                limit :offset , :limit';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':title', '%' . $title . '%');
        $cmd->bindValue(':offset', $page * $pageSize);
        $cmd->bindValue(':limit', $pageSize);
        $items = DbCacheUtility::queryAll($cmd);
        return $items;
    }

    public static function getById($id)
    {
        $sql = 'SELECT * FROM t_sale_article WHERE id = :id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);
        $item = DbCacheUtility::queryOne($cmd);
        return $item;
    }

    public static function save($id, $title, $detail_html)
    {
        $item = self::getById($id);
        if ($item == null) {
            //insert
            $sql = 'INSERT INTO t_sale_article (id, detail_html, is_enabled, title) 
                    VALUES (uuid(), :detail_html, :is_enabled, :title)';

            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':detail_html', $detail_html);
            $cmd->bindValue(':is_enabled', '0');
            $cmd->bindValue(':title', $title);

            return DbCacheUtility::execute($cmd) > 0;
        }

        $sql = 'UPDATE t_sale_article SET detail_html = :detail_html, title = :title WHERE id = :id ';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':detail_html', $detail_html);
        $cmd->bindValue(':title', $title);
        $cmd->bindValue(':id', $id);

        return DbCacheUtility::execute($cmd) > 0;
    }

    /**
     * 更改上传图片位置
     * @param $info
     * @return mixed
     */
    public static function saveProductInfoImage($info)
    {
        //正则匹配出img
        $rel = preg_match('/<img.*?src=[\"|\']?(\/upload\/\d{8}\/\d+?\.(jpg|gif|bmp|bnp|png))[\"|\']?\s.*?>/', $info, $match);
        //检测图片
        if ($rel) {
            //取出src地址
            $path_info = pathinfo($match[1]);
            $img_path = $_SERVER['DOCUMENT_ROOT'] . $match[1];

            //上传至服务器
            $file_save_name = Tools::uuid() . '.' . $path_info['extension'];
            $url = FileService::uploadImage($img_path, $file_save_name);

            //特殊符号转义
            $src = quotemeta($match[1]);
            $src = preg_replace('/\//', '\/', $src, -1);
//            $url = preg_replace('/\//', '\/', $url, -1);

            //src替换
            $info = preg_replace('/(<img.*?src=[\"|\']?)' . $src . '([\"|\']?\s.*?>)/', '${1}' . $url . '${2}', $info);
            $info = self::saveProductInfoImage($info);
        }
        return $info;

    }

    /**
     * 修改宝典状态
     * @param $id
     * @return bool
     */
    public static function modifyStatus($id){
        $sql = 'UPDATE t_sale_article SET is_enabled = 1 - is_enabled WHERE id = :id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id',$id);
        return DbCacheUtility::execute($cmd) > 0 ;
    }


}